


#include <stdlib.h>
#include <inttypes.h>
#include <math.h>
#include "ab.h"
#include "../Functions/cube_functions.h"
#include "../Functions/rotation.h"
#include "../Functions/translation.h"
#include "../Animations/animations.h"
#include "../Animations/shapes.h"



volatile const unsigned char bitmaps[6][8] = {
	{0xc3,0xc3,0x00,0x18,0x18,0x81,0xff,0x7e}, // smiley 3 small
	{0x3c,0x42,0x81,0x81,0xc3,0x24,0xa5,0xe7}, // Omega
	{0x00,0x04,0x06,0xff,0xff,0x06,0x04,0x00},  // Arrow
	{0x81,0x42,0x24,0x18,0x18,0x24,0x42,0x81}, // X
	{0xBD,0xA1,0xA1,0xB9,0xA1,0xA1,0xA1,0x00}, // ifi
	{0xEF,0x48,0x4B,0x49,0x4F,0x00,0x00,0x00} // TG
};

const unsigned char paths[44] = {0x07,0x06,0x05,0x04,0x03,0x02,0x01,0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x67,0x57,0x47,0x37,0x27,0x17,
0x04,0x03,0x12,0x21,0x30,0x40,0x51,0x62,0x73,0x74,0x65,0x56,0x47,0x37,0x26,0x15}; // circle, len 16, offset 28


char String2BackTemp[8][8];

unsigned char reverseBits(unsigned char num) {
	unsigned int count = sizeof(num) * 8 - 1;
	unsigned int reverse_num = num;
	num >>= 1;

	while(num){
		reverse_num <<= 1;
		reverse_num |= num & 1;
		num >>= 1;
		count--;
	}
	reverse_num <<= count;
	return reverse_num;

}

void cube_char( char ch, int threeD ){

	  unsigned long h;
	  int i,j;


	  if(ch < 32)ch = ' ';


	  h = (ch<<3);

	  j = 7;
	  for (i = 0; i < 8; i++){
		  cube[j][7] = reverseBits(font[h+i]);
		  if (threeD)cube[j][6] = cube[j][7];
		   j--;
	  }
	  rotate_90_deg();
}

void cube_char_b2f_init( char ch){


	  unsigned long h;
	  int i,j;



	  if(ch < 32)ch = ' ';


	  h = (ch<<3);

	  j = 7;
	  for (i = 0; i < 8; i++){

		  cube[j][0] = reverseBits(font[h+i]);
		  String2BackTemp[j][0] = cube[j][0];
		  j--;

	  }

	  rotate_90_deg();
}

void cube_char_b2f( int layers ){
	int i,k;





	  for (k=(layers-2);k<layers;k++){
		  for(i = 8; i > 0; i--){
			  cube[i-1][k] = String2BackTemp[i-1][0];
		  }
	  }

	  rotate_90_deg();



}


void font_getchar (char chr, unsigned char dst[5])
{
	int i;

    for (i = 0; i < 8; i++)
		dst[i] = (unsigned char)font[(chr*8)+i];
}

void font_getpath (unsigned char path, unsigned char *destination, int length)
{
	int i;
	int offset = 0;

	if (path == 1)
		offset=28;

	for (i = 0; i < length; i++)
		destination[i] = paths[i+offset];
}

void font_getbitmap (char bitmap, unsigned char dst[8])
{
	int i;

    for (i = 0; i < 8; i++)
		dst[i] = (unsigned char)bitmaps[(int)bitmap][(int)i];

}

unsigned char font_getbitmappixel ( char bitmap, char x, char y)
{
	int tmp = bitmaps[(int)bitmap][(int)x];
	return (tmp >> y) & 0x01;
}


